﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PointLight.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

namespace GameKits.Unity.Rts.UnitComponents
{
    using UnityEngine;

    public class PointLight : UnitComponent
    {
        /// <summary>
        /// El componente luz
        /// </summary>
        private Light pointLight;

        /// <summary>
        /// El gameObject donde estará la luz
        /// </summary>
        private GameObject lightObject;

        /// <summary>
        /// La posición relativa a la unidad del foco
        /// </summary>
        private Vector2 lightPosition = new Vector3(0, 10, 0);

        /// <summary>
        /// El rango de la luz
        /// </summary>
        private float lightRange = 10;

        /// <summary>
        /// Indica si muestra o no la luz
        /// </summary>
        private bool showLight = true;

        /// <summary>
        /// Gets or sets LightPosition.
        /// </summary>
        public Vector2 LightPosition
        {
            get
            {
                return this.lightPosition;
            }

            set
            {
                if (this.lightPosition != value)
                {
                    this.lightPosition = value;
                    this.SetDirty();
                }
            }
        }

        /// <summary>
        /// Gets or sets LightRange.
        /// </summary>
        public float LightRange
        {
            get
            {
                return this.lightRange;
            }

            set
            {
                if (this.lightRange != value)
                {
                    this.lightRange = value;
                    this.SetDirty();
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether ShowLight.
        /// </summary>
        public bool ShowLight
        {
            get
            {
                return this.showLight;
            }

            set
            {
                if (this.showLight != value)
                {
                    this.showLight = value;
                    this.SetDirty();
                }
            }
        }

        /// <summary>
        /// Inicializamos el componente
        /// </summary>
        protected override void OnInitialize()
        {
            this.lightObject = new GameObject("Light");
            this.lightObject.transform.parent = this.ParentUnit.Transform;
            this.lightObject.transform.localPosition = this.LightPosition;

            this.pointLight = this.lightObject.AddComponent<Light>();
            this.pointLight.range = this.LightRange;
            this.pointLight.enabled = this.ShowLight;
        }

        /// <summary>
        /// Actualizamos las propiedades del componente
        /// </summary>
        protected override void OnUpdateSettings()
        {
            this.lightObject.transform.localPosition = this.LightPosition;
            this.pointLight.range = this.LightRange;
            this.pointLight.enabled = this.ShowLight;
        }
    }
}
